package PComprehensiveTest1;

import java.util.ArrayList;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        boolean flag = true;
        ArrayList<Student> message = new ArrayList<>();
        while (flag)
        {
            System.out.println("-----请输入你选择的工作项目-----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.println("请输入你的选择：");
            Scanner sc = new Scanner(System.in);
            int choose = sc.nextInt();
            switch (choose) {
                case 1 -> {
                    System.out.println("你选择了1");
                    boolean middle=addStudent(message);
                    if(middle)
                        System.out.println("添加成功");
                    else
                        System.out.println("添加失败");
                }
                case 2 -> {
                    System.out.println("你选择了2");
                    deleteStudent(message);
                }
                case 3 -> {
                    System.out.println("你选择了3");
                    updateStudent(message);
                }
                case 4 -> {
                    System.out.println("你选择了4");
                    queryStudent(message);
                }
                case 5 -> {
                    System.out.println("你选择了5,程序结束");
                    flag = exit();
                }
                default -> System.out.println("没有这个选项功能的提供");
            }
        }
    }

    //添加学生
    public static boolean addStudent(ArrayList<Student> message) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生学号");
        String id = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生地址");
        String address = sc.next();
        Student student = new Student(name, id, age, address);
        if(getIndex(message,id)!=-1)
        {
            System.out.println("该学生已存在");
            return false;
        }
        message.add(student);
        return true;
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> message) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号");
        String id = sc.next();
        int index = getIndex(message,id);
        if(index==-1)
            System.out.println("该学生不存在");
        else {
            message.remove(index);
            System.out.println("删除成功");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> message) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生的学号");
        String id = sc.next();
        int index = getIndex(message,id);
        if(index==-1)
            System.out.println("该学生不存在");
        else {
            System.out.println("请输入学生姓名");
            String name = sc.next();    //姓名
            System.out.println("请输入学生年龄");
            int age = sc.nextInt();    //年龄
            System.out.println("请输入学生地址");
            String address = sc.next();//地址
            Student student = new Student(name, id, age, address);
            message.set(index,student);
            System.out.println("修改成功");
        }
    }
    //查询学生
    public static void queryStudent(ArrayList<Student> message) {
        if (message.isEmpty())
            System.out.println("当前无学生信息，请添加后再查询");
        else {
            for (int i = 0; i < message.size(); i++) {
                Student student = message.get(i);
                System.out.println("姓名：" + student.getName() + " 学号：" + student.getId() + " 年龄：" + student.getAge() + " 地址：" + student.getAddress());
            }
        }
    }
    //退出系统
    public static boolean exit() {
        return false;
    }


    //获取id的下标的方法
    public static int getIndex(ArrayList<Student> message,String id)
    {
        int index = -1;
        for (int i = 0; i < message.size(); i++) {
            if(message.get(i).getId().equals(id))
            {
                index = i;
                break;
            }
        }
        return index;
    }
}
